﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SWEN_HMS.Classes;

namespace SWEN_HMS.BookingModule
{
    public partial class Request : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["staffid"] == null)
            {
                Response.Redirect("~/UserModule/Login.aspx");
            }
            else if ((Convert.ToString(Session["accessLvl"])) == "No Access")
            {
                Response.Redirect("~/UserModule/Login.aspx");
            }
            else
            {
                if (Request.QueryString["Room"] != null)
                {
                    string roomNo = Request.QueryString["Room"];
                    Label1.Text = roomNo;
                }
            }
        }

        protected void requestType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (requestType.SelectedValue == "Room Service")
            {
                category.Enabled = true;
                Panel2.Visible = true;
            }
            else
            {
                Panel2.Visible = false;
                category.Enabled = false;
                category.Text = "Select Option";
            }
        }

        protected void category_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (category.SelectedValue != null)
            {
                SqlDataSource1.SelectCommand = "Select * From Item Where category Like'" + category.SelectedValue + "%'";
                GridView1.Visible = true;
                quantity.Enabled = true;
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (GridView1.SelectedRow != null)
            {
                GridViewRow row = GridView1.SelectedRow;
                itemID.Text = row.Cells[1].Text;
                sItem.Text = row.Cells[2].Text;
                desc.Text = row.Cells[3].Text;
                unitPrice.Text = row.Cells[4].Text;
            }
        }

        protected void confirm_Click(object sender, EventArgs e)
        {
            string check = RoomDBManager.checkForCheckInStatus(Convert.ToInt32(Label1.Text));
            if (check == "True")
            {
                if (requestType.SelectedValue == "Room Service")
                {
                    int id = Convert.ToInt32(Label1.Text);

                    //this method is to retrieve the bookingID based on the roomNo
                    int bk = RoomDBManager.getBookingID(id);

                    //this method is to retrieve the invoiceID based on the bookingID
                    int invoiceID = RoomDBManager.getInvoiceID(bk);

                    //store the item requested into InvoiceItem
                    InvoiceItem i = new InvoiceItem();
                    i.invoiceID = invoiceID;
                    i.quantity = Convert.ToInt32(quantity.Text);
                    i.unitPrice = Convert.ToDecimal(unitPrice.Text);
                    i.itemID = Convert.ToInt32(itemID.Text);
                    i.description = desc.Text;

                    //add the request into the addInvoiceItem method
                    RoomDBManager.addInvoiceItem(i);
                    System.Windows.Forms.MessageBox.Show("Guest request have been made");
                    Response.Redirect("~/BookingModule/RoomAvailability.aspx");
                }
                else if (requestType.SelectedValue == "Cleaning")
                {
                    //get the roomStatus of the room based on the roomNo 
                    string result = RoomDBManager.checkRoomStatus(Convert.ToInt32(Label1.Text));

                    if (result == "Cleaning")
                    {
                        System.Windows.Forms.MessageBox.Show("Cleaning is in progress");
                        Response.Redirect("~/BookingModule/RoomAvailability.aspx");
                    }
                    else
                    {
                        //change the roomStatus based on the roomNo and also the roomStatus
                        RoomDBManager.changeRoomStatus(Convert.ToInt32(Label1.Text), requestType.SelectedValue);
                        System.Windows.Forms.MessageBox.Show("Guest request have been made");
                        Response.Redirect("~/BookingModule/RoomAvailability.aspx");
                    }

                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("Please select request type");
                }
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("Invalid request");
                Response.Redirect("~/BookingModule/RoomAvailability.aspx");
            }

        }

    }
}